package com.example.sentinelapp.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author sjw
 * @className HomeController
 * @date 2021/12/21 15:15
 * @description: TODO
 */
@RestController
@RequestMapping("/home")
public class HomeController {
    @SentinelResource(value = "hello", blockHandler = "exceptionHandler")
    @GetMapping("/hello")
    public String hello() {
        System.out.println("hello");
        return "hello";
    }

    // Block 异常处理函数，参数最后多一个 BlockException，其余与原函数一致.
    public String exceptionHandler(BlockException ex) {
        // Do some log here.
        ex.printStackTrace();
        return String.format("error: hello  is not OK");
    }


    @SentinelResource(value = "hello1", blockHandler = "exceptionHandler1")
    @GetMapping("/hello1")
    public String hello1() {
        System.out.println("hello1");
        return "hello1";
    }

    // Block 异常处理函数，参数最后多一个 BlockException，其余与原函数一致.
    public String exceptionHandler1(BlockException ex) {
        // Do some log here.
        ex.printStackTrace();
        return String.format("error: hello1  is not OK");
    }
}
